import 'package:flutter/material.dart';
import 'package:flutter_axrjc/datasource/network/bean/EventBean.dart';
import 'package:flutter_axrjc/datasource/network/request/network_service.dart';
import 'package:flutter_axrjc/main.dart';
import 'package:flutter_axrjc/page/web_view/logic.dart';
import 'package:flutter_axrjc/routes.dart';
import 'package:flutter_axrjc/style.dart';
import 'package:flutter_axrjc/util/CommonPage.dart';
import 'package:flutter_axrjc/widget/CachePage.dart';
import 'package:get/get.dart';

import 'logic.dart';

class MinePage extends StatelessWidget {
  const MinePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final logic = Get.find<MineLogic>();
    final state = Get.find<MineLogic>().state;

    return CachePage(
      child: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(15),
          child: SingleChildScrollView(
            child: Column(
              children: [
                const SizedBox(
                  height: 20,
                ),
                InkWell(
                  onTap: () {
                    if (state.user.value == null) {
                      Get.toNamed(MainRoutes.login)?.then((value) {
                        logic.loadUser();
                      });
                    } else {
                      Get.toNamed(MainRoutes.profile);
                    }
                  },
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Row(
                        children: [
                          Obx(() {
                            return CircleAvatar(
                              radius: 24,
                              backgroundColor: Colors.white,
                              backgroundImage: state.user.value == null
                                  ? Image.asset('assets/defaultAvatar.png')
                                      .image
                                  : NetworkImage(
                                      "${state.user.value!.customerInfo?.avatar}"),
                            );
                          }),
                          const SizedBox(
                            width: 10,
                          ),
                          Obx(() {
                            return Text(
                              state.user.value == null
                                  ? "请登录/注册"
                                  : "欢迎你，${state.user.value?.customerInfo?.nickName}",
                              style: const TextStyle(
                                  fontSize: 16, fontWeight: FontWeight.bold),
                            );
                          }),
                        ],
                      ),
                      const Row(
                        children: [
                          Text(
                            "个人信息",
                            style: TextStyle(
                                fontSize: 12, color: Color(0xff999999)),
                          ),
                          Icon(
                            Icons.keyboard_arrow_right_sharp,
                            color: Color(0xff999999),
                            size: 10,
                          )
                        ],
                      ),
                    ],
                  ),
                ),
                Stack(
                  children: [
                    Column(
                      children: [
                        const SizedBox(
                          height: 20,
                        ),
                        Container(
                          padding: const EdgeInsets.symmetric(
                              horizontal: 10, vertical: 20),
                          decoration: BoxDecoration(
                              gradient: LinearGradient(
                                  begin: Alignment.topRight,
                                  end: Alignment.bottomLeft,
                                  colors: [
                                    const Color(0xffF7D25D).withAlpha(76),
                                    const Color(0xffE3801D).withAlpha(76),
                                  ]),
                              borderRadius: BorderRadius.circular(20)),
                        ),
                      ],
                    ),
                    Positioned(
                        left: 5,
                        top: -8,
                        bottom: -15,
                        child: Stack(
                          children: [
                            Container(
                              color: Colors.transparent,
                              child: Image.asset(
                                "assets/main/jifen.png",
                                fit: BoxFit.cover,
                              ),
                            ),
                            const Positioned.fill(
                                child: Center(
                              child: Text(
                                "我的积分",
                                style: TextStyle(
                                    fontSize: 12, color: Colors.white),
                              ),
                            ))
                          ],
                        )),
                    Positioned(
                      top: 20,
                      right: 20,
                      bottom: 0,
                      child: Center(child: Obx(() {
                        return Text(
                          state.user.value == null
                              ? "登录后查看"
                              : "${state.user.value?.customerInfo?.userIntegral}",
                          style:
                              const TextStyle(fontSize: 12, color: orangeColor),
                        );
                      })),
                    ),
                  ],
                ),
                const SizedBox(
                  height: 10,
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(vertical: 15),
                  child: Row(
                    children: [
                      Expanded(
                          child: InkWell(
                        onTap: () {
                          logic.toMyLight();
                        },
                        child: Container(
                          height: 80,
                          decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(10)),
                          child: Row(
                            children: [
                              const Expanded(
                                  child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Padding(
                                    padding: EdgeInsets.all(8.0),
                                    child: Text("我的灯具"),
                                  ),
                                ],
                              )),
                              Image.asset(
                                "assets/light.png",
                                scale: 2,
                              ),
                              const SizedBox(
                                width: 10,
                              )
                            ],
                          ),
                        ),
                      )),
                      const SizedBox(
                        width: 10,
                      ),
                      Expanded(
                          child: InkWell(
                        onTap: () {
                          Get.toNamed(MainRoutes.noticeSetting);
                        },
                        child: Container(
                          height: 80,
                          decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(10)),
                          child: Row(
                            children: [
                              const Expanded(
                                  child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Padding(
                                    padding: EdgeInsets.all(8.0),
                                    child: Text("提醒设置"),
                                  ),
                                ],
                              )),
                              Image.asset("assets/main/notice.png"),
                              const SizedBox(
                                width: 13,
                              )
                            ],
                          ),
                        ),
                      )),
                    ],
                  ),
                ),
                // ItemBar(
                //     title: "我的发布",
                //     asset: "assets/main/mine/mypost.png",
                //     onTap: () {
                //       Get.toNamed(MainRoutes.myPost);
                //     }),
                ItemBar(
                    title: "签到",
                    asset: "assets/main/mine/task1.png",
                    scale: 8,
                    onTap: () {
                      // InkWell(
                      //   onTap: () {
                      //     Get.toNamed(MainRoutes.attendance)
                      //         ?.then((value) {
                      //       if (value == "01") {
                      //         eventBus.fire(EventBean(
                      //             type: EventType.changeMainPage,
                      //             data: 0));
                      //       }
                      //       if (value == "02") {
                      //         eventBus.fire(EventBean(
                      //             type: EventType.changeMainPage,
                      //             data: 2));
                      //       }
                      //     });
                      //   },
                      //   child: Padding(
                      //     padding: const EdgeInsets.all(8.0),
                      //     child: Column(
                      //       children: [
                      //         Image.asset(
                      //           'assets/main/qiandao.png',
                      //           scale: 2,
                      //         ),
                      //         const Text(
                      //           "签到",
                      //           style: TextStyle(fontSize: 12),
                      //         )
                      //       ],
                      //     ),
                      //   ),
                      // ),
                      Get.toNamed(MainRoutes.attendance)?.then((value) {
                        if (value == "01") {
                          eventBus.fire(EventBean(
                              type: EventType.changeMainPage, data: 0));
                        }
                        if (value == "02") {
                          eventBus.fire(EventBean(
                              type: EventType.changeMainPage, data: 2));
                        }
                      });
                    }),
                ItemBar(
                    title: "用户协议",
                    asset: "assets/main/mine/userAgreement.png",
                    onTap: () {
                      showWebView(WebViewConfig(
                          type: WebViewConfigType.url,
                          urlOrHtml:
                              "${NetworkService.currentBaseUrl}app/processSysConfigView/1",
                          title: "用户协议"));
                    }),
                ItemBar(
                    title: "隐私政策",
                    asset: "assets/main/mine/privacyPolicy.png",
                    onTap: () {
                      showWebView(WebViewConfig(
                          type: WebViewConfigType.url,
                          urlOrHtml:
                              "${NetworkService.currentBaseUrl}app/processSysConfigView/2",
                          title: "隐私政策"));
                    }),
                ItemBar(
                    title: "帮助中心",
                    asset: "assets/main/mine/help.png",
                    onTap: () {
                      Get.toNamed(MainRoutes.helpCenter);
                    }),
                ItemBar(
                    title: "用户反馈及建议",
                    asset: "assets/main/mine/feedback.png",
                    onTap: () {
                      Get.toNamed(MainRoutes.feedback);
                    }),
                ItemBar(
                    title: "关于我们",
                    asset: "assets/main/mine/about.png",
                    onTap: () {
                      showWebView(WebViewConfig(
                          type: WebViewConfigType.url,
                          urlOrHtml:
                              "${NetworkService.currentBaseUrl}app/processSysConfigView/4",
                          title: "关于我们"));
                    }),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class ItemBar extends StatelessWidget {
  final String title;
  final String asset;
  final double scale;
  final GestureTapCallback? onTap;

  const ItemBar(
      {super.key,
      required this.title,
      required this.asset,
      this.onTap,
      this.scale = 3});

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                Image.asset(asset, scale: scale),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(title),
                ),
              ],
            ),
            const Icon(
              Icons.keyboard_arrow_right_sharp,
              color: Colors.grey,
            )
          ],
        ),
      ),
    );
  }
}
